<?php
//**** This file needs to be included from a file that has access to "wp-load.php" ****

function wl_handle_subsc_signup($ipn_data,$subsc_ref,$unique_ref)
{	
	$postURL = get_option('eStore_wishlist_post_url');// the post URL	
	$secretKey = get_option('eStore_wishlist_secret_word');	// the Secret Key
	if(empty($unique_ref)){$unique_ref = $ipn_data['txn_id'];}
	
	// prepare the data
	$data = array ();
	$data['cmd'] = 'CREATE';
	$data['transaction_id'] = $unique_ref;
	$data['lastname'] = $ipn_data['last_name'];
	$data['firstname'] = $ipn_data['first_name'];
	$data['email'] = $ipn_data['payer_email'];
	$data['level'] = $subsc_ref;
	
	debug_log_subsc("WL Member signup debug data: ".$data['cmd']."|".$data['transaction_id']."|".$data['lastname']."|".$data['firstname']."|".$data['email']."|".$data['level'],true);
	debug_log_subsc("WL Member signup Post URL: ".$postURL,true);
	
	// generate the hash
	$delimiteddata = strtoupper (implode ('|', $data));
	$hash = md5 ($data['cmd'] . '__' . $secretKey . '__' . $delimiteddata);
	
	// include the hash to the data to be sent
	$data['hash'] = $hash;
	
	// send data to post URL
	$ch = curl_init ($postURL);
	curl_setopt ($ch, CURLOPT_POST, true);
	curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	$returnValue = curl_exec ($ch);
	
	// process return value
	list ($cmd, $url) = explode ("\n", $returnValue);
	
	// check if the returned command is the same as what we passed
	if ($cmd == 'CREATE') 
	{
	    $message = $url;
	} 
	else 
	{
	    $message = "Error returned from WishList plugin! Command value:".$cmd.". Error details: ".$url;
	}
	debug_log_subsc("Member signup URL :".$message,true);
	
	// Send the membership signup email
	$email_subj = "Complete your membership registration";
	$to_address = $ipn_data['payer_email'];
	$from_address = get_option('eStore_download_email_address');
	$email_body = "Dear ".$ipn_data['first_name'].
				  "\n\nPlease Visit the following URL to complete your registration: ".
				  "\n".$message.
				  "\n\nThank You";
	$headers = 'From: '.$from_address . "\r\n";
	    
	if (get_option('eStore_use_wp_mail'))
    {
        wp_mail($to_address,$email_subj,$email_body,$headers);
        debug_log_subsc("Member signup email successfully sent using wp mail system to:".$to_address,true);
    }
    else
    {
		$attachment='';
		if(@eStore_send_mail($to_address,$email_body,$email_subj,$headers))
		{
		    debug_log_subsc("Member signup email successfully sent (using PHP mail) to:".$to_address,true);
		}
		else
		{
		    debug_log_subsc("Member signup email sending failed (using PHP mail) ",false);
		}
    }
}  
function wl_handle_subsc_cancel($ipn_data,$refund=false)
{
	// the post URL
	$postURL = get_option('eStore_wishlist_post_url');
	// the Secret Key
	$secretKey = get_option('eStore_wishlist_secret_word');
	
	// prepare the data
	$data = array ();
	$data['cmd'] = 'DEACTIVATE';
	if($refund){
		$data['transaction_id'] = $ipn_data['parent_txn_id'];
	}
	else{
		$data['transaction_id'] = $ipn_data['subscr_id'];
	}
	
	// generate the hash
	$delimiteddata = strtoupper (implode ('|', $data));
	$hash = md5 ($data['cmd'] . '__' . $secretKey . '__' . $delimiteddata);
	// include the hash to the data to be sent
	$data['hash'] = $hash;
	// send data to post URL
	$ch = curl_init ($postURL);
	curl_setopt ($ch, CURLOPT_POST, true);
	curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	$returnValue = curl_exec ($ch);
	// process return value
	list ($cmd, $url) = explode ("\n", $returnValue);
	// check if the returned command is the same as what we passed
	if ($cmd == 'DEACTIVATE') 
	{
		$message = "Membership deactivated successfully";
	} else 
	{
		$message = "Membership deactivation failed";
	}
	debug_log_subsc($message,true);	
}

function eMember_handle_subsc_signup($ipn_data,$subsc_ref,$unique_ref,$eMember_id='')
{
    global $wpdb,$emember_config;
    $members_table_name = $wpdb->prefix . "wp_eMember_members_tbl";
    $membership_level_table = $wpdb->prefix . "wp_eMember_membership_tbl";
    	
    if(empty($eMember_id))
    {	
    	debug_log_subsc("eMember ID is empty. Checking the database for existing record of this email address..",true);
	    $email = $ipn_data['payer_email'];
	    $query_db = $wpdb->get_row("SELECT * FROM $members_table_name WHERE email = '$email'", OBJECT);
	    if($query_db){
	    	$eMember_id = $query_db->member_id;
	    	debug_log_subsc("Found record in the members table. The account will be upgraded. Member ID: ".$eMember_id,true);
	    }
	    else{
	    	debug_log_subsc("Did not find record in the members table. A new member account will be created for: ".$email,true);
	    }
    }
	if (!empty($eMember_id))//Update the existing member account
	{
		// upgrade the member account or assign new membership levels to his/her profile
		debug_log_subsc("Upgrading member account",true);
		$account_state = 'active';
		$membership_level = $subsc_ref;
		$subscription_starts = (date ("Y-m-d"));
		$subscr_id = $unique_ref;
		$resultset = "";
		if($emember_config->getValue('eMember_enable_secondary_membership'))
		{
			debug_log_subsc("Using secondary membership level feature... adding additional levels to the existing profile of member ID:".$eMember_id,true);
			debug_log_subsc("Quering the table :".$members_table_name.", to retrieve member profile",true);
			$resultset = $wpdb->get_row("SELECT * FROM $members_table_name where member_id='$eMember_id'", OBJECT);
			if($resultset)
			{
				debug_log_subsc("Retrieved member profile. Need to add membership level ID: ".$membership_level.", to this profile",true);
				$additional_levels = $resultset->more_membership_levels;
				debug_log_subsc("Current additional levles for this profile: ".$additional_levels,true);
				if(is_null($additional_levels))
				{					
					$additional_levels = $resultset->membership_level; //assign the current primary level to the additional level
					debug_log_subsc("Current additional levels for this profile is null. Adding level: ".$additional_levels,true);
				}
				else if(empty($additional_levels))
				{					
					$additional_levels = $resultset->membership_level;	//assign the current primary level to the additional level		
					debug_log_subsc("Current additional levels for this profile is empty. Adding level: ".$additional_levels,true);		
				}
				else
				{					
					$additional_levels = $additional_levels.",".$resultset->membership_level; //add the current primary level to the list of additional levels
					$sec_levels = explode(',', $additional_levels);
					$additional_levels = implode(',', array_unique($sec_levels));//make sure there is no duplicate entry
					debug_log_subsc("New additional level set: ".$additional_levels,true);
				}
				debug_log_subsc("Updating additional levels column for username: ".$resultset->user_name." with value: ".$additional_levels,false);
				$updatedb = "UPDATE $members_table_name SET more_membership_levels='$additional_levels' WHERE member_id='$eMember_id'";    	    	
				$results = $wpdb->query($updatedb);		

				debug_log_subsc("Upgrading the primary membership level to the recently paid level. New primary membership level ID for this member is: ".$membership_level,true);
				$updatedb = "UPDATE $members_table_name SET account_state='$account_state',membership_level='$membership_level',subscription_starts='$subscription_starts',subscr_id='$subscr_id' WHERE member_id='$eMember_id'";    	    	
				$results = $wpdb->query($updatedb);
							
			}
			else
			{
				debug_log_subsc("Could not find a member account for the given eMember ID",false);
			}
		}
		else
		{
			debug_log_subsc("Not using secondary membership level feature... upgrading the current membership level of member ID: ".$eMember_id,true);
			$updatedb = "UPDATE $members_table_name SET account_state='$account_state',membership_level='$membership_level',subscription_starts='$subscription_starts',subscr_id='$subscr_id' WHERE member_id='$eMember_id'";    	    	
			$results = $wpdb->query($updatedb);			
		}	
				
    	//If using the WP user integration then update the role on WordPress too
    	if($emember_config->getValue('eMember_create_wp_user'))
    	{
			debug_log_subsc("Updating WordPress user role...",true);
			$resultset = $wpdb->get_row("SELECT * FROM $members_table_name where member_id='$eMember_id'", OBJECT);
    		$membership_level = $resultset->membership_level;
    		$username = $resultset->user_name;    		
	        $membership_level_resultset = $wpdb->get_row("SELECT * FROM $membership_level_table where id='$membership_level'", OBJECT);
	                                                                         
			$wp_user_id = $username;
	        $wp_user_info  = array();
	        $wp_user_info['user_nicename'] = $username;
	        $wp_user_info['display_name']  = $username;
	        $wp_user_info['nickname']      = $username;
	        $wp_user_info['user_email']    = $resultset->email;
	        //$wp_user_info['user_pass']     = $_POST['pwd'];                       
	        $wp_user_info['ID']            = $wp_user_id;
	        $wp_user_info['role']            = $membership_level_resultset->role;
	        $wp_user_info['user_registered'] = date('Y-m-d H:i:s');                                        
	        wp_update_user($wp_user_info);	        
	        $wp_user = get_userdatabylogin( $username ); 
	        $wp_user_id = $wp_user->ID;
	        debug_log_subsc("Current users username :".$username." ,Membership level is: ".$membership_level." WP User ID is: ".$wp_user_id,true);
	        update_wp_user_Role($wp_user_id, $membership_level_resultset->role); 	        
	        do_action( 'set_user_role', $wp_user_id, $membership_level_resultset->role );	        
	        debug_log_subsc("Current WP users role updated to: ".$membership_level_resultset->role,true);
    	} 
	    			
		// Set "notify email address" to the member's email address
		$resultset = $wpdb->get_row("SELECT * FROM $members_table_name where member_id='$eMember_id'", OBJECT);
    	$email = $resultset->email;	//$email = $ipn_data['payer_email'];	
    	debug_log_subsc("Setting the TO EMAIL address for membership upgrade notification to: ".$email,true);		
    	    	
	    $subject = $emember_config->getValue('eMember_account_upgrade_email_subject');
	    if (empty($subject))
	    {
	    	$subject = WP_ESTORE_EMEMBER_ACCOUNT_UPGRADE_SUBJECT;
	    }    	
    	$body = $emember_config->getValue('eMember_account_upgrade_email_body');
    	if (empty($body))
    	{
    		$body = WP_ESTORE_EMEMBER_ACCOUNT_UPGRADE_BODY;
    	}
		$from_address = get_option('senders_email_address');
		//$email_body = $body;
		$login_link = $emember_config->getValue('login_page_url');
		$tags1 = array("{first_name}","{last_name}","{user_name}","{login_link}");			
		$vals1 = array($resultset->first_name,$resultset->last_name,$resultset->user_name,$login_link);			
		$email_body = str_replace($tags1,$vals1,$body);			
	    $headers = 'From: '.$from_address . "\r\n";    	
	}//End of existing member account update
	else
	{
		// create fresh new member account
		debug_log_subsc("Creating new member account",true);		
		$user_name ='';
		$password = '';
	
		$first_name = $ipn_data['first_name'];
		$last_name = $ipn_data['last_name'];
		$email = $ipn_data['payer_email'];
		$membership_level = $subsc_ref;
		$subscr_id = $unique_ref;
		
	    $address_street = $ipn_data['address_street'];
	    $address_city = $ipn_data['address_city'];
	    $address_state = $ipn_data['address_state'];
	    $address_zipcode = $ipn_data['address_zip'];
	    $country = $ipn_data['address_country'];
	
		$date = (date ("Y-m-d"));
		$account_state = 'active';
		$reg_code = uniqid(); //rand(10, 1000);
		$md5_code = md5($reg_code);
	
	    $updatedb = "INSERT INTO $members_table_name (user_name,first_name,last_name,password,member_since,membership_level,account_state,last_accessed,last_accessed_from_ip,email,address_street,address_city,address_state,address_zipcode,country,gender,referrer,extra_info,reg_code,subscription_starts,txn_id,subscr_id) VALUES ('$user_name','$first_name','$last_name','$password', '$date','$membership_level','$account_state','$date','IP','$email','$address_street','$address_city','$address_state','$address_zipcode','$country','','','','$reg_code','$date','','$subscr_id')";
	    $results = $wpdb->query($updatedb);
	
		$results = $wpdb->get_row("SELECT * FROM $members_table_name where subscr_id='$subscr_id' and reg_code='$reg_code'", OBJECT);
	
		$id = $results->member_id;
		
	    $separator='?';
		$url=get_option('eMember_registration_page');
		if(strpos($url,'?')!==false)
		{
			$separator='&';
		}
		$reg_url = $url.$separator.'member_id='.$id.'&code='.$md5_code;
		debug_log_subsc("Member signup URL :".$reg_url,true);
	
		$subject = get_option('eMember_email_subject');
		$body = get_option('eMember_email_body');
		$from_address = get_option('senders_email_address');
	
	    $tags = array("{first_name}","{last_name}","{reg_link}");
	    $vals = array($first_name,$last_name,$reg_url);
		$email_body    = str_replace($tags,$vals,$body);
	    $headers = 'From: '.$from_address . "\r\n";
	}
    if (get_option('eStore_use_wp_mail'))
    {
        wp_mail($email,$subject,$email_body,$headers);
        debug_log_subsc("Member signup/upgrade completion email successfully sent to:".$email." From email address value used:".$from_address,true);
    }
    else
    {
    	$attachment='';
    	if(@eStore_send_mail($email,$email_body,$subject,$from_address,$attachment))
    	{
    	    debug_log_subsc("Member signup/upgrade completion email successfully sent (using PHP mail) to:".$email." From email address value used:".$from_address,true);
    	}
    	else
    	{
    	    debug_log_subsc("Member signup/upgrade completion email sending failed (using PHP mail) ",false);
    	}
    }
}

function eMember_handle_subsc_cancel($ipn_data,$refund=false)
{
	if($refund){		
		$subscr_id = $ipn_data['parent_txn_id'];
		debug_log_subsc("Refund notification check for eMember - check if a member account needs to be deactivated... subscr ID: ".$subscr_id,true); 
	}
	else{		
		$subscr_id = $ipn_data['subscr_id']; 
		debug_log_subsc("Subscription cancellation notification check for eMember - check if a member account needs to be deactivated... subscr ID: ".$subscr_id,true);
	}	
    //$subscr_id = $ipn_data['subscr_id'];    

    global $wpdb;
    $members_table_name = $wpdb->prefix . "wp_eMember_members_tbl";
    $membership_level_table   = $wpdb->prefix . "wp_eMember_membership_tbl";
    
    debug_log_subsc("Retrieving member account from the database...",true);
    $resultset = $wpdb->get_row("SELECT * FROM $members_table_name where subscr_id='$subscr_id'", OBJECT);
    if($resultset)
    {
    	$membership_level = $resultset->membership_level;
    	$level_query = $wpdb->get_row("SELECT * FROM $membership_level_table where id='$membership_level'", OBJECT);
    	if ($level_query->subscription_period == 0)
    	{
    		//subscription duration is set to no expiry or until canceled so deactivate the account now
    		$account_state = 'inactive';
		    $updatedb = "UPDATE $members_table_name SET account_state='$account_state' WHERE subscr_id='$subscr_id'";
		    $results = $wpdb->query($updatedb);    		
		    debug_log_subsc("Subscription cancellation received! Member account deactivated.",true);
    	}
    	else
    	{
    		//Set the account to unsubscribed and it will be set to inactive when the "Subscription duration" is over	
    		$account_state = 'unsubscribed';    
		    $updatedb = "UPDATE $members_table_name SET account_state='$account_state' WHERE subscr_id='$subscr_id'";
		    $results = $wpdb->query($updatedb);    		
		    debug_log_subsc("Subscription cancellation received! Member account set to unsubscribed.",true);
    	}
    }
    else
    {
    	debug_log_subsc("No member found for the given subscriber ID:".$subscr_id,false);
    	return;
    }      
}

function debug_log_subsc($message,$success,$end=false)
{
    // Timestamp
    $text = '['.date('m/d/Y g:i A').'] - '.(($success)?'SUCCESS :':'FAILURE :').$message. "\n";
    if ($end) {
    	$text .= "\n------------------------------------------------------------------\n\n";
    }
    // Write to log
    $debug_log_file_name = realpath(dirname(__FILE__))."/subscription_handle_debug.log";
    $fp=fopen($debug_log_file_name,'a');
    fwrite($fp, $text );
    fclose($fp);  // close file
}
?>